<?php

class SalesService extends BaseService {

	public function saveOrder($salesDetails, $employee) {
		$result = $this->isProductAvailable($salesDetails);
		
		$ret['result'] = true;
		
		if ($result['result']) {		
			$salesManager = new SalesManager();
	
			$sales = new Sales_Domain();
	
			$sales->setTotalAmount($this->getTotalPrice($salesDetails));
			$sales->setTotalQty($this->getTotalProducts($salesDetails));
			$sales->setEmployee($employee);
			$sales->setDateSold(date("Y-m-d H:i:s"));
			
			$salesManager->setObj($sales);
	
			$salesManager->add();
			
			$sales_id = $salesManager->getResult()->getSalesId();
	
			$sales->setSalesId($sales_id);
	
			$sales->setSalesDetails($salesDetails);
	
			$salesDetailsManager = new SalesDetailsManager();
				
			$salesDetailsManager->setObj($sales);
	
			$salesDetailsManager->add();
			
			$this->updateProductQty($salesDetails);
		} else {
			$ret['result'] = false;			
			$ret['product'] = $result['product'];
		}
		
		return $ret;
	}

	private function getTotalProducts($salesDetails) {
		$total = 0;

		foreach ($salesDetails as $saleDetails) {
			$total += $saleDetails->getProductQty();
		}

		return $total;
	}

	private function getTotalPrice($salesDetails) {
		$total = 0;

		foreach ($salesDetails as $saleDetails) {
			$total += $saleDetails->getTotalPrice();
		}

		return $total;
	}

	private function isProductAvailable($salesDetails) {
		$ret['result'] = true;
		$ret['product'] = null;

		foreach ($salesDetails as $saleDetails) {
			$productManager = new ProductManager();
			
			$product_qty = $saleDetails->getProductQty();
			
			$productManager->setObj($saleDetails->getProduct());
			
			$productManager->query();
			
			$product = $productManager->getResult();
			
			if ($product_qty > $product->getProductQty()) {
				$ret['result'] = false;
				$ret['product'] = $product;
				
				break;
			}
		}

		return $ret;
	}
	
	private function updateProductQty($salesDetails) {
		foreach ($salesDetails as $saleDetails) {
			$productManager = new ProductManager();
			
			$product = $saleDetails->getProduct();

			$productManager->setObj($product);
			
			$productManager->query();
			
			$product = $productManager->getResult();
			
			$newQty = $product->getProductQty() - $saleDetails->getProductQty();
			
			$product->setProductQty($newQty);
				
			$productManager->setObj($product);
				
			$productManager->update();
		}
	}
	
	public function getTodaySales() {
		$salesManager = new SalesManager();
		
		$salesManager->getTodaySales();
		
		$results = $salesManager->getResult();
		
		return $results;
	}
	
	public function getSalesDetails($sales_id) {		
		$salesDetailsManager = new SalesDetailsManager();
		$sales = new Sales_Domain();
		
		$salesDetailsManager = new SalesDetailsManager();
		
		$filter['sales_id'] = $sales_id;
		
		$salesDetailsManager->setFilter($filter);
		
		$salesDetailsManager->query();
		
		$results = $salesDetailsManager->getResult();
		
		$sales->setSalesId($sales_id);
		$sales->setSalesDetails($results);
		
		return $sales;
	}
	
	public function getSalesDetail($sales_details_id) {
		$salesDetailsManager = new SalesDetailsManager();
		$sales = new Sales_Domain();
	
		$salesDetailsManager = new SalesDetailsManager();
	
		$filter['sales_detail_id'] = $sales_details_id;
	
		$salesDetailsManager->setFilter($filter);
	
		$salesDetailsManager->query();
	
		$results = $salesDetailsManager->getResult();
	
		return $results;
	}
}